package com.topdt.jbpm.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.service.BaseManager;
import com.topdt.jbpm.dao.ApplicationDao;
import com.topdt.jbpm.dao.ControlDao;
import com.topdt.jbpm.dao.FieldDao;
import com.topdt.jbpm.model.Application;
import com.topdt.jbpm.model.Control;
import com.topdt.jbpm.model.Field;

/**
 * 建立日期 : 2013-6-20 <br>
 * 作者 : 武海峰<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 */
@Service
@Transactional(readOnly = true)
public class ApplicationService extends BaseManager<Application>{

	@Autowired
	private ApplicationDao applicationDao;
	@Autowired
	private FieldDao fieldDao;
	@Autowired
	private ControlDao controlDao;
	@Transactional(readOnly = false)
	public void saveOrUpdate(final Application app) {
		applicationDao.saveOrUpdate(app);
	}

	public Application findById(String appId) {
		return applicationDao.getById(appId);
	}


	@Transactional(readOnly = false)
	public void save(Application entity) {
		super.save(entity);
	}

	/**
	 * 获取application模板数据并插入表里
	 * 
	 * @param companyId
	 */
	@Transactional(readOnly = false)
	public void addApp(String companyId) {
		this.applicationDao.addApp(companyId);
	}

	/**
	 * 扩展应用列表
	 * 
	 * @param comId
	 * @return
	 */
	public List<Application> listExtApps(String comId,String appType) {
		return applicationDao.listApps(comId, "0",appType);
	}

	/**
	 * 固有应用列表
	 * 
	 * @param comId
	 * @return
	 */
	public List<Application> listInApps(String comId,String appType) {
		return applicationDao.listApps(comId, "1",appType);
	}
	
	/**
	 * 通过应用类型查找应用
	 * @author 冯晋首
	 * @date   2016年3月18日 下午4:38:53 
	 * @param comId
	 * @param appType
	 * @return
	 */
	public List<Application> findAppsByType(String comId, String appType) {
		return applicationDao.findAppsByType(comId, appType);
	}
	
	//2014-12-02 add wangxufeng
	public List<Application> getAppsByComId(String comId){
		return applicationDao.getAppsByComId(comId);
	}
	//--通过名字找app
	public Application getAppName(String appName, String companyId) {
		return applicationDao.getAppName(appName, companyId);
	}
	/**
	 * 
	* @author _zili
	* @Description:
	* @date 2015年3月30日 上午10:11:54
	 */
	public List<Application> getAppsPageByComId(String appName){
		return applicationDao.getAppsPageByComId(appName);
	}
	/**
	 * 
	* @author _zili
	* @Description:判断应用是否能用
	* @date 2015年5月11日 上午9:45:26
	 */
	public String judgeApp(String appId){
		Application application = applicationDao.get(appId);
		String flag = "1";
		if(application!=null&&application.getForm()!=null){
			List<Field> fList = fieldDao.getFieldsByformId(application.getForm().getFormId());
			String appAttr = application.getAppAttr();
			//是否有字段,除了外置表单、外置公文、外置表单和意见、外置公文和意见
			if((fList==null||fList.size()==0)&&!(appAttr.equals(Application.APPATTR_CUSTOM_FORM) 
					|| appAttr.equals(Application.APPATTR_CUSTOM_FORM_OPINION) 
					|| appAttr.equals(Application.APPATTR_CUSTOM_DOC) 
					|| appAttr.equals(Application.APPATTR_CUSTOM_DOC_OPINION))){
				
				flag = "0";
			}
 			Object[] objs = new Object[] { appId, application.getFlowId(), application.getForm().getFormId() };
			List<Control> listControl = controlDao.findControlByObjects(objs);
			if((listControl==null||listControl.size()==0)&&StringUtils.isNotBlank(application.getFlowId())){//是否有字段读写性权限
				flag = "0";
			}else{
				//是否有表单模板预览,除了自定义表单外
				if((application.getForm().getTempletByte()==null)&&!(appAttr.equals(Application.APPATTR_CUSTOM_FORM) 
						|| appAttr.equals(Application.APPATTR_CUSTOM_FORM_OPINION) 
						|| appAttr.equals(Application.APPATTR_CUSTOM_DOC) 
						|| appAttr.equals(Application.APPATTR_CUSTOM_DOC_OPINION))){
					flag = "0";
				}
				//是否有流程操作权限
				if(listControl!=null&&StringUtils.isBlank(listControl.get(0).getFunctionControl())&&StringUtils.isNotBlank(application.getFlowId())){
					flag = "0";
				}
			}
		}else{
			flag = "0";
		}
		return flag;
	}
	public List<Application> findAppsByType(String typeId) {
		return applicationDao.findAppsByType(typeId);
	}
}
